<?php

namespace Admin\Controller;
class FreeAgreementController extends CommonController{
	public  $exec_comfunc  = 'index,add,edit,delete,del,recycle';//index,add,edit,delete
	
	public function _initialize(){
		parent::_initialize();
		$type = $this->_get('type');
		$this->_db_where['type'] = $type;
		$id = $this->_get('id');
		if(ACTION_NAME == 'add'){
			// 跳转URL
			$this->_last_id_field = 'id';
			$this->_jump_url = U('edit', array('type' => $type));
		}
		
	}
	public function index(){
		// 按类别搜索
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = $category_id;
		}
		// 按标题搜索
		$title = $this->_get('title');
		if($title){
			$where['title'] = array('like', '%' . $title . '%');
		}
		$this->assign('title', $title);
		
		$this->assign('info', $where);
		$where['is_delete'] = 0;
		parent::index('', $where);
	}
	// 问答设置
	public function ask_setting(){
		$agreement_id = $this->_get('id', '对不起，您的操作有误！');

		$where['id'] = $agreement_id;
		$info = D('FreeAgreement')->where($where)->find();

		if(IS_POST){
			$ask_data  = $this->_post('ask_data', '请设置问答！');
			$step_num  = count($ask_data);
			$ask_data  = serialize($ask_data);
			$step_data = serialize($this->_post('step_data'));
			// 清空问答
			D('FreeAgreementAsk')->where(array('agreement_id' => $agreement_id))->delete();
			// 添加问答
			$data['agreement_id'] = $agreement_id;
			$data['ask_data']     = $ask_data;
			$data['step_data']    = $step_data;
			$data['step_num']     = $step_num;
			$data['type']         = $info['type'];
			$data['admin_id']     = $this->_user_id;
			$data['create_time']  = time();
			D('FreeAgreementAsk')->add($data);

			$this->success('保存成功！');
		}

		$ask_info  = D('FreeAgreementAsk')->where(array('agreement_id' => $agreement_id))->find();
		$ask_data  = $ask_info['ask_data'] ? unserialize($ask_info['ask_data']) : array();
		$step_data = $ask_info['step_data'] ? unserialize($ask_info['step_data']) : array();

		$this->assign('info', $info);
		$this->assign('ask_data', $ask_data);
		$this->assign('step_data', $step_data);
		$this->display();
	}
	// 内容设置
	public function content_setting(){
		$id   = $this->_get('id');
		$type = $this->_get('type');
		$this->_use_tpl  = 'content_setting';
		$this->_jump_url = U('content_setting', array('type' => $type, 'id' => $id));

		$this->assign('label_list', D('FreeAgreement')->get_label_list($id));
		parent::edit();
	}
}